package com.cindata

import com.cindata.dictionary.*

class ValuationHistory
{
    String name
    Date valuationDate
    BigDecimal unitPrice
    BigDecimal totalPrice

    ExecStatusDict evaluateStatus
    EvaluateDict evaluateType

    String sourceClass
    String sourceId

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User user
    User createBy
    User modifyBy

    String evaluateCode
    String failReason

    Account account

    static mapping = {
        autowire true
    }

    static constraints = 
    {
        name maxSize: 50, nullable: true, blank: true

        valuationDate nullable: true, blank: true
        unitPrice precision: 15, scale: 2, nullable: true, blank: true
        totalPrice precision: 15, scale: 2, nullable: true, blank: true

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        evaluateStatus nullable: true, blank: true
        evaluateType nullable: true, blank: true

        sourceClass maxSize: 50, nullable: true, blank: true
        sourceId maxSize: 50, nullable: true, blank: true

        evaluateCode maxSize: 50, nullable: true, blank: true
        failReason maxSize: 200, nullable: true, blank: true

        account nullable: true, blank: true
    }

    def springSecurityService
    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)

        if (!account) 
        {
            account = createBy?.account
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
